import { Timer } from "@sapi-game/gameComponent/common/timer/timer";
import { GameComponent, GameState } from "@sapi-game/main";
import { WereWolfContext } from "@/werewolf/context";
import { WereWolfPlayer } from "@/werewolf/player";

interface timeLimiterOptions {
    onEnd: () => void;
    getPlayers: () => WereWolfPlayer[];
}

export class WereWolfTimeLimiter extends GameComponent<
    GameState<WereWolfPlayer, WereWolfContext>,
    timeLimiterOptions
> {
    override onAttach(): void {
        const timer = this.state.getComponent(Timer);
        if (!timer || !this.options) return;
        this.subscribe(timer.events.tick, (t) => {
            if (this.context.map.showTime)
                this.options?.getPlayers()?.forEach((p) => {
                    p.actionbar(`剩余时间 : ${t.remainingTime} 秒`);
                });
        });
        this.subscribe(timer.events.onTime, () => this.options?.onEnd(), {
            time: 0,
            once: false,
        });
    }
}
